package edu.sctu.yueding.activitys;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import edu.sctu.yueding.R;
import edu.sctu.yueding.bean.JsonBaseBeen;
import edu.sctu.yueding.bean.UserBean;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.config.URLConfig;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.ProgressUtil;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;
import edu.sctu.yueding.utils.httputils.SharePreference;
import edu.sctu.yueding.view.CircleImageView;

/**
 * Created by zcy on 2016/8/24.
 */
public class ScanResultAddFriendsActivity extends BaseActivity implements View.OnClickListener {


    private TextView back, name, self, phone;
    private String result;
    private Activity activity = this;
    private ProgressUtil progressUtil;
    private ProgressDialog progressDialog;
    private CircleImageView circleImageView;
    private Button addFriendBtn;
    private Long friendId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_scan_friend);
        initSystemBar(this, 0);
        initView();
        clickEvent();
    }

    private void initView() {
        addFriendBtn = (Button) findViewById(R.id.add_friend);

        back = (TextView) findViewById(R.id.id_personal_data_back_txt);
        circleImageView = (CircleImageView) findViewById(R.id.id_my_team_team_portrait);
        name = (TextView) findViewById(R.id.id_my_team_member_username);

        self = (TextView) findViewById(R.id.id_friends_intro);

        phone = (TextView) findViewById(R.id.id_my_team_member_phone);

        result = getIntent().getStringExtra("result");
        progressUtil = ProgressUtil.getInstance(activity, null);

        progressDialog = progressUtil.getProgressDialog(ProgressDialog.STYLE_SPINNER, true);
        if (result != null && !result.equals("")) {

            friendId = Long.valueOf(result.split("/")[result.split("/").length-3]);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    getFriendInfo(result);
                }
            }, 1000);
        }

    }

    private void clickEvent() {

        back.setOnClickListener(this);
        addFriendBtn.setOnClickListener(this);


    }


    private void getFriendInfo(String result) {

        Log.e("result",result);

        GetVolleyHttp.getInstance(this).getStringGETJsonData(result, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Log.e("re", response);

                progressUtil.dismiss(progressDialog);
                JsonBaseBeen<UserBean> userBeenJsonBaseBeen = JSON.parseObject(response, new TypeReference<JsonBaseBeen<UserBean>>() {
                }.getType());

                if (userBeenJsonBaseBeen.getData() != null) {

                    GetVolleyHttp.getInstance(activity).setImageLoader(circleImageView, R.drawable.user_default_portrait, R.drawable.error_image, userBeenJsonBaseBeen.getData().getAvatarUrl());
                    name.setText(userBeenJsonBaseBeen.getData().getNickname());
                    self.setText(userBeenJsonBaseBeen.getData().getSelfIntroduction());
                    phone.setText(userBeenJsonBaseBeen.getData().getLogin());


                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressUtil.dismiss(progressDialog);
                Toast.makeText(activity, "网络出了点问题！", Toast.LENGTH_SHORT).show();
            }
        }, "Bearer " + SharePreference.getInstance(activity).getString(CacheConfig.CACHE_TOKEN));

    }

    private void addFriend() {

        GetVolleyHttp.getInstance(this).getStringGETJsonData(URLConfig.addFriend(friendId), new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressUtil.dismiss(progressDialog);
                JsonBaseBeen<String> re = JSON.parseObject(response, new TypeReference<JsonBaseBeen<String>>() {
                }.getType());
                Bundle bundle = new Bundle();

                if (re.getMeta().code == 200) {
                    JumpUtils.jumpActivity(activity, MyFriendsActivity.class);
                    Toast.makeText(activity, "成功添加好友，可在我的好友里查看", Toast.LENGTH_SHORT).show();
                    activity.finish();
                } else if (re.getMeta().code == 40000400) {

                    bundle.putString("errorMsg", "不能添加自己");
                    JumpUtils.jumpActivity(activity, ScanErrorActivity.class, bundle);
                    activity.finish();
                } else if (re.getMeta().code == 40000401) {

                    bundle.putString("errorMsg", "不能重复添加好友");
                    JumpUtils.jumpActivity(activity, ScanErrorActivity.class, bundle);
                    activity.finish();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressUtil.dismiss(progressDialog);
                Toast.makeText(activity, "网络出了点问题！", Toast.LENGTH_SHORT).show();
            }
        }, "Bearer " + SharePreference.getInstance(activity).getString(CacheConfig.CACHE_TOKEN));

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {

            Bundle bundle = new Bundle();
            bundle.putInt("flag", 3);
            JumpUtils.activityToFragmentJump(activity, bundle);
            this.finish();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.id_personal_data_back_txt:

                Bundle bundle = new Bundle();
                bundle.putInt("flag", 3);
                JumpUtils.activityToFragmentJump(activity, bundle);
                finish();
                break;

            case R.id.add_friend:

                Log.e("te", "fdskgkfb");

                progressDialog = progressUtil.getProgressDialog(ProgressDialog.STYLE_SPINNER, true);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        addFriend();
                    }
                }, 1000);

                break;
        }
    }
}
